import java.util.concurrent.CountDownLatch;

public class Foo {
    public static void main(String[] args) {

    }

    // 第一个门闩，控制second在first之后执行
    private CountDownLatch firstDone = new CountDownLatch(1);
    // 第二个门闩，控制third在second之后执行
    private CountDownLatch secondDone = new CountDownLatch(1);

    public Foo() {

    }

    public void first() {
        // 打印"first"
        System.out.print("first");
        // 计数减1，通知等待firstDone的线程可以继续
        firstDone.countDown();
    }

    public void second() {
        try {
            // 等待first()执行完毕
            firstDone.await();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            return;
        }
        // 打印"second"
        System.out.print("second");
        // 计数减1，通知等待secondDone的线程可以继续
        secondDone.countDown();
    }

    public void third() {
        try {
            // 等待second()执行完毕
            secondDone.await();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            return;
        }
        // 打印"third"
        System.out.print("third");
    }
}
